package controller;

import java.util.List;

import model.ChiTietDichGia_Sach;
import model.ChiTietDichGia_SachID;
import model.ChiTietNXB_Sach;
import model.ChiTietNXB_SachID;
import model.ChiTietTacGia_Sach;
import model.ChiTietTacGia_SachID;
import model.DichGia;
import model.NhaXuatBan;
import model.Sach;
import model.TacGia;
import model.TheLoai;

import org.hibernate.criterion.Restrictions;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Textbox;

import dao.entity.ChiTietDichGia_SachDAO;
import dao.entity.ChiTietNXB_SachDAO;
import dao.entity.ChiTietTacGia_SachDAO;
import dao.entity.DichGiaDAO;
import dao.entity.NhaXuatBanDAO;
import dao.entity.SachDAO;
import dao.entity.TacGiaDAO;
import dao.entity.TheLoaiDAO;

@SuppressWarnings("serial")
public class AddBook extends SelectorComposer<Component> {
	@Wire
	private Textbox tenSach, giaBan, soTrang, soLuong, hinhAnh, moTa;
	@Wire
	private Combobox theLoai, tacGia, dichGia, nhaXuatBan;
	private TheLoai theLoaiModel;
	private Sach sachModel;
	private TacGia tacGiaModel;
	private DichGia dichGiaModel;
	private TheLoai theLoaiModel2;
	private NhaXuatBan nhaXuatBanModel;
	private SachDAO sachDAO;
	private TheLoaiDAO theloaiDAO;
	private TacGiaDAO tacGiaDAO;
	private DichGiaDAO dichGiaDAO;
	private NhaXuatBanDAO nhaXuatBanDAO;
	private List<TheLoai> listTheLoai;
	private List<TacGia> listTacGia, listTacGiaUpdate;
	private List<DichGia> listDichGia, listDichGiaUpdate;
	private List<NhaXuatBan> listNXB, listNXBUpdate;
	private List<Sach> listSach;

	public AddBook() {
		sachDAO = new SachDAO();
		theloaiDAO = new TheLoaiDAO();
		tacGiaDAO = new TacGiaDAO();
		dichGiaDAO = new DichGiaDAO();
		nhaXuatBanDAO = new NhaXuatBanDAO();
	}

	@Listen("onClick=#themSach")
	public void themSach() {
		String tl = theLoai.getValue();
		listTheLoai = theloaiDAO.findByCriteria(Restrictions.eq("tenTheLoai",
				tl));
		int giaBan_int = Integer.parseInt(giaBan.getText());
		int soTrang_int = Integer.parseInt(soTrang.getText());
		int soLuong_int = Integer.parseInt(soLuong.getText());
		if (listTheLoai.size() == 0) {
			theLoaiModel = new TheLoai(tl);
			sachModel = new Sach(tenSach.getText(), giaBan_int,
					hinhAnh.getText(), soTrang_int, moTa.getText(),
					soLuong_int, theLoaiModel);
			sachModel.setSoLuongBan(1);
			theloaiDAO.persist(theLoaiModel);
			sachDAO.persist(sachModel);
			System.out.println("Khong trung");
		} else {
			System.out.println("id = " + listTheLoai.get(0).getId_TheLoai());
			theLoaiModel2 = new TheLoai(listTheLoai.get(0).getId_TheLoai());
			sachModel = new Sach(tenSach.getText(), giaBan_int,
					hinhAnh.getText(), soTrang_int, moTa.getText(),
					soLuong_int, theLoaiModel2);
			sachModel.setSoLuongBan(1);
			System.out.println("Trùng thể loại");
			sachDAO.persist(sachModel);
		}
		// Insert Tác Giả
		insertTacGia();
		insertDichGia();
		insertNXB();

	}

	private void insertNXB() {
		String tenNXB = nhaXuatBan.getValue();
		listNXB = nhaXuatBanDAO.findByCriteria(Restrictions
				.eq("tenNXB", tenNXB));
		if (listNXB.size() == 0) {
			System.out.println("Không trùng nxb");
			nhaXuatBanModel = new NhaXuatBan(tenNXB);
			nhaXuatBanDAO.persist(nhaXuatBanModel);
			listNXBUpdate = nhaXuatBanDAO.findByCriteria(Restrictions.eq(
					"tenNXB", tenNXB));
			listSach = sachDAO.findByCriteria(Restrictions.eq("tenSach",
					tenSach.getText()));
			ChiTietNXB_SachID nhaXuatBan_SachID = new ChiTietNXB_SachID(
					listSach.get(0).getId_Sach(), listNXBUpdate.get(0)
							.getId_NXB());
			ChiTietNXB_Sach nxb_sach = new ChiTietNXB_Sach(nhaXuatBan_SachID);
			ChiTietNXB_SachDAO nxb_sachDAO = new ChiTietNXB_SachDAO();
			nxb_sachDAO.persist(nxb_sach);

		} else {
			System.out.println("Trung nxb");
			listNXBUpdate = nhaXuatBanDAO.findByCriteria(Restrictions.eq(
					"tenNXB", tenNXB));
			listSach = sachDAO.findByCriteria(Restrictions.eq("tenSach",
					tenSach.getText()));
			ChiTietNXB_SachID nhaXuatBan_SachID = new ChiTietNXB_SachID(
					listSach.get(0).getId_Sach(), listNXBUpdate.get(0)
							.getId_NXB());
			ChiTietNXB_Sach nxb_sach = new ChiTietNXB_Sach(nhaXuatBan_SachID);
			ChiTietNXB_SachDAO nxb_sachDAO = new ChiTietNXB_SachDAO();
			nxb_sachDAO.persist(nxb_sach);
		}
	}

	private void insertDichGia() {
		String tenDG = dichGia.getValue();
		listDichGia = dichGiaDAO.findByCriteria(Restrictions.eq("ten_DichGia",
				tenDG));
		if (listDichGia.size() == 0) {
			dichGiaModel = new DichGia(tenDG);
			dichGiaDAO.persist(dichGiaModel);
			listDichGiaUpdate = dichGiaDAO.findByCriteria(Restrictions.eq(
					"ten_DichGia", tenDG));
			listSach = sachDAO.findByCriteria(Restrictions.eq("tenSach",
					tenSach.getText()));
			System.out.println("Không trùng dịch giả");
			ChiTietDichGia_SachID dichGia_SachID = new ChiTietDichGia_SachID(
					listSach.get(0).getId_Sach(), listDichGiaUpdate.get(0)
							.getId_DichGia());
			ChiTietDichGia_Sach dichGia_Sach = new ChiTietDichGia_Sach(
					dichGia_SachID);
			ChiTietDichGia_SachDAO dichGia_SachDAO = new ChiTietDichGia_SachDAO();
			dichGia_SachDAO.persist(dichGia_Sach);

		} else {
			System.out.println("trùng dịch giả");
			listDichGiaUpdate = dichGiaDAO.findByCriteria(Restrictions.eq(
					"ten_DichGia", tenDG));
			listSach = sachDAO.findByCriteria(Restrictions.eq("tenSach",
					tenSach.getText()));
			ChiTietDichGia_SachID dichGia_SachID = new ChiTietDichGia_SachID(
					listSach.get(0).getId_Sach(), listDichGiaUpdate.get(0)
							.getId_DichGia());
			ChiTietDichGia_Sach dichGia_Sach = new ChiTietDichGia_Sach(
					dichGia_SachID);
			ChiTietDichGia_SachDAO dichGia_SachDAO = new ChiTietDichGia_SachDAO();
			dichGia_SachDAO.persist(dichGia_Sach);
		}

	}

	private void insertTacGia() {
		String tenTG = tacGia.getValue();
		listTacGia = tacGiaDAO.findByCriteria(Restrictions.eq("tenTacGia",
				tenTG));
		if (listTacGia.size() == 0) {
			tacGiaModel = new TacGia(tenTG);
			tacGiaDAO.persist(tacGiaModel);
			listTacGiaUpdate = tacGiaDAO.findByCriteria(Restrictions.eq(
					"tenTacGia", tenTG));
			listSach = sachDAO.findByCriteria(Restrictions.eq("tenSach",
					tenSach.getText()));
			System.out.println("Không trùng tác giả");
			ChiTietTacGia_SachID tacGia_SachID = new ChiTietTacGia_SachID(
					listSach.get(0).getId_Sach(), listTacGiaUpdate.get(0)
							.getId_TacGia());
			ChiTietTacGia_Sach tacgGia_Sach = new ChiTietTacGia_Sach(
					tacGia_SachID);
			ChiTietTacGia_SachDAO tacGia_SachDAO = new ChiTietTacGia_SachDAO();
			tacGia_SachDAO.persist(tacgGia_Sach);
		} else {
			System.out.println("Trùng tac giả");
			listTacGiaUpdate = tacGiaDAO.findByCriteria(Restrictions.eq(
					"tenTacGia", tenTG));
			listSach = sachDAO.findByCriteria(Restrictions.eq("tenSach",
					tenSach.getText()));
			ChiTietTacGia_SachID tacGia_SachID = new ChiTietTacGia_SachID(
					listSach.get(0).getId_Sach(), listTacGiaUpdate.get(0)
							.getId_TacGia());
			ChiTietTacGia_Sach tacgGia_Sach = new ChiTietTacGia_Sach(
					tacGia_SachID);
			ChiTietTacGia_SachDAO tacGia_SachDAO = new ChiTietTacGia_SachDAO();
			tacGia_SachDAO.persist(tacgGia_Sach);
		}

	}

	public static void main(String[] args) {
		String tenSach = "booknew";
		SachDAO sach = new SachDAO();
		List<Sach> listSach = sach.findByCriteria(Restrictions.eq("tenSach",
				tenSach));
		for (int i = 0; i < listSach.size(); i++) {
			System.out.println(listSach.get(i).getTenSach());
		}
	}
}
